home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zcharout.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  10.1 KB  |  388 lines

  1. /* Copyright (C) 1996, 1997, 1999, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zcharout.c,v 1.2.2.2 2000/10/26 12:45:11 igorm Exp $ */
  20. /* Common code for outline (Type 1 / 4 / 42) fonts */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "oper.h"
  24. #include "gscrypt1.h"
  25. #include "gstext.h"
  26. #include "gxdevice.h"        /* for gxfont.h */
  27. #include "gxfont.h"
  28. #include "gxfont1.h"
  29. #include "dstack.h"        /* only for systemdict */
  30. #include "estack.h"
  31. #include "ichar.h"
  32. #include "icharout.h"
  33. #include "idict.h"
  34. #include "ifont.h"
  35. #include "igstate.h"
  36. #include "iname.h"
  37. #include "store.h"
  38.  
  39. /*
  40.  * Execute an outline defined by a PostScript procedure.
  41.  * The top elements of the stack are:
  42.  *      <font> <code|name> <name> <outline_id>
  43.  */
  44. int
  45. zchar_exec_char_proc(i_ctx_t *i_ctx_p)
  46. {
  47.     os_ptr op = osp;
  48.     /*
  49.      * The definition is a PostScript procedure.  Execute
  50.      *      <code|name> proc
  51.      * within a systemdict begin/end and a font begin/end.
  52.      */
  53.     es_ptr ep;
  54.  
  55.     check_estack(5);
  56.     ep = esp += 5;
  57.     make_op_estack(ep - 4, zend);
  58.     make_op_estack(ep - 3, zend);
  59.     ref_assign(ep - 2, op);
  60.     make_op_estack(ep - 1, zbegin);
  61.     make_op_estack(ep, zbegin);
  62.     ref_assign(op - 1, systemdict);
  63.     {
  64.     ref rfont;
  65.  
  66.     ref_assign(&rfont, op - 3);
  67.     ref_assign(op - 3, op - 2);
  68.     ref_assign(op - 2, &rfont);
  69.     }
  70.     pop(1);
  71.     return o_push_estack;
  72. }
  73.  
  74. /*
  75.  * Get the metrics for a character from the Metrics dictionary of a base
  76.  * font.  If present, store the l.s.b. in psbw[0,1] and the width in
  77.  * psbw[2,3].
  78.  */
  79. int                /*metrics_present*/
  80. zchar_get_metrics(const gs_font_base * pbfont, const ref * pcnref,
  81.           double psbw[4])
  82. {
  83.     const ref *pfdict = &pfont_data(pbfont)->dict;
  84.     ref *pmdict;
  85.  
  86.     if (dict_find_string(pfdict, "Metrics", &pmdict) > 0) {
  87.     ref *pmvalue;
  88.  
  89.     check_type_only(*pmdict, t_dictionary);
  90.     check_dict_read(*pmdict);
  91.     if (dict_find(pmdict, pcnref, &pmvalue) > 0) {
  92.         if (num_params(pmvalue, 1, psbw + 2) >= 0) {    /* <wx> only */
  93.         psbw[3] = 0;
  94.         return metricsWidthOnly;
  95.         } else {
  96.         int code;
  97.  
  98.         check_read_type_only(*pmvalue, t_array);
  99.         switch (r_size(pmvalue)) {
  100.             case 2:    /* [<sbx> <wx>] */
  101.             code = num_params(pmvalue->value.refs + 1,
  102.                       2, psbw);
  103.             psbw[2] = psbw[1];
  104.             psbw[1] = psbw[3] = 0;
  105.             break;
  106.             case 4:    /* [<sbx> <sby> <wx> <wy>] */
  107.             code = num_params(pmvalue->value.refs + 3,
  108.                       4, psbw);
  109.             break;
  110.             default:
  111.             return_error(e_rangecheck);
  112.         }
  113.         if (code < 0)
  114.             return code;
  115.         return metricsSideBearingAndWidth;
  116.         }
  117.     }
  118.     }
  119.     return metricsNone;
  120. }
  121.  
  122. /* Get the vertical metrics for a character from Metrics2, if present. */
  123. int
  124. zchar_get_metrics2(const gs_font_base * pbfont, const ref * pcnref,
  125.            double pwv[4])
  126. {
  127.     const ref *pfdict = &pfont_data(pbfont)->dict;
  128.     ref *pmdict;
  129.  
  130.     if (dict_find_string(pfdict, "Metrics2", &pmdict) > 0) {
  131.     ref *pmvalue;
  132.  
  133.     check_type_only(*pmdict, t_dictionary);
  134.     check_dict_read(*pmdict);
  135.     if (dict_find(pmdict, pcnref, &pmvalue) > 0) {
  136.         check_read_type_only(*pmvalue, t_array);
  137.         if (r_size(pmvalue) == 4) {
  138.         int code = num_params(pmvalue->value.refs + 3, 4, pwv);
  139.  
  140.         return (code < 0 ? code : metricsSideBearingAndWidth);
  141.         }
  142.     }
  143.     }
  144.     return metricsNone;
  145. }
  146.  
  147. /*
  148.  * Consult Metrics2 and CDevProc, and call setcachedevice[2].  Return
  149.  * o_push_estack if we had to call a CDevProc, or if we are skipping the
  150.  * rendering process (only getting the metrics).
  151.  */
  152. int
  153. zchar_set_cache(i_ctx_t *i_ctx_p, const gs_font_base * pbfont,
  154.         const ref * pcnref, const double psb[2],
  155.         const double pwidth[2], const gs_rect * pbbox,
  156.         op_proc_t cont_fill, op_proc_t cont_stroke,
  157.         const double Metrics2_sbw_default[4])
  158. {
  159.     os_ptr op = osp;
  160.     const ref *pfdict = &pfont_data(pbfont)->dict;
  161.     ref *pcdevproc;
  162.     int have_cdevproc;
  163.     ref rpop;
  164.     bool metrics2;
  165.     op_proc_t cont;
  166.     double w2[10];
  167.     gs_text_enum_t *penum = op_show_find(i_ctx_p);
  168.  
  169.     w2[0] = pwidth[0], w2[1] = pwidth[1];
  170.  
  171.     /* Adjust the bounding box for stroking if needed. */
  172.  
  173.     w2[2] = pbbox->p.x, w2[3] = pbbox->p.y;
  174.     w2[4] = pbbox->q.x, w2[5] = pbbox->q.y;
  175.     if (pbfont->PaintType == 0)
  176.     cont = cont_fill;
  177.     else {
  178.     double expand = max(1.415, gs_currentmiterlimit(igs)) *
  179.     gs_currentlinewidth(igs) / 2;
  180.  
  181.     w2[2] -= expand, w2[3] -= expand;
  182.     w2[4] += expand, w2[5] += expand;
  183.     cont = cont_stroke;
  184.     }
  185.  
  186.     /* Check for Metrics2. */
  187.  
  188.     {
  189.     int code = zchar_get_metrics2(pbfont, pcnref, w2 + 6);
  190.  
  191.     if (code < 0)
  192.         return code;
  193.     metrics2 = code > 0;
  194.     }
  195.  
  196.     /*
  197.      * For FontType 9 and 11, if Metrics2 is missing, the caller provides
  198.      * default Metrics2 values derived from the FontBBox.
  199.      */
  200.     if (!metrics2 && Metrics2_sbw_default != NULL) {
  201.         w2[6] = Metrics2_sbw_default[2];
  202.         w2[7] = Metrics2_sbw_default[3];
  203.         w2[8] = Metrics2_sbw_default[0];
  204.         w2[9] = Metrics2_sbw_default[1];
  205.     metrics2 = true;
  206.     }
  207.  
  208.     /* Check for CDevProc or "short-circuiting". */
  209.  
  210.     have_cdevproc = dict_find_string(pfdict, "CDevProc", &pcdevproc) > 0;
  211.     if (have_cdevproc || zchar_show_width_only(penum)) {
  212.     int i;
  213.     op_proc_t zsetc;
  214.     int nparams;
  215.  
  216.     if (have_cdevproc) {
  217.         check_proc_only(*pcdevproc);
  218.         zsetc = zsetcachedevice2;
  219.         if (!metrics2) {
  220.         w2[6] = w2[0], w2[7] = w2[1];
  221.         w2[8] = w2[9] = 0;
  222.         }
  223.         nparams = 10;
  224.     } else {
  225.         make_oper(&rpop, 0, zpop);
  226.         pcdevproc = &rpop;
  227.         if (metrics2)
  228.         zsetc = zsetcachedevice2, nparams = 10;
  229.         else
  230.         zsetc = zsetcachedevice, nparams = 6;
  231.     }
  232.     check_estack(3);
  233.     /* Push the l.s.b. for .type1addpath if necessary. */
  234.     if (psb != 0) {
  235.         push(nparams + 3);
  236.         make_real(op - (nparams + 2), psb[0]);
  237.         make_real(op - (nparams + 1), psb[1]);
  238.     } else {
  239.         push(nparams + 1);
  240.     }
  241.     for (i = 0; i < nparams; ++i)
  242.         make_real(op - nparams + i, w2[i]);
  243.     ref_assign(op, pcnref);
  244.     push_op_estack(cont);
  245.     push_op_estack(zsetc);
  246.     ++esp;
  247.     ref_assign(esp, pcdevproc);
  248.     return o_push_estack;
  249.     } {
  250.     int code =
  251.         (metrics2 ? gs_text_setcachedevice2(penum, w2) :
  252.          gs_text_setcachedevice(penum, w2));
  253.  
  254.     if (code < 0)
  255.         return code;
  256.     }
  257.  
  258.     /* No metrics modification, do the stroke or fill now. */
  259.  
  260.     /* Push the l.s.b. for .type1addpath if necessary. */
  261.     if (psb != 0) {
  262.     push(2);
  263.     make_real(op - 1, psb[0]);
  264.     make_real(op, psb[1]);
  265.     }
  266.     return cont(i_ctx_p);
  267. }
  268.  
  269. /*
  270.  * Get the CharString data corresponding to a glyph.  Return typecheck
  271.  * if it isn't a string.
  272.  */
  273. private bool charstring_is_notdef_proc(P1(const ref *));
  274. private int charstring_make_notdef(P2(gs_const_string *, const gs_font *));
  275. int
  276. zchar_charstring_data(gs_font *font, const ref *pgref, gs_const_string *pstr)
  277. {
  278.     ref *pcstr;
  279.  
  280.     if (dict_find(&pfont_data(font)->CharStrings, pgref, &pcstr) <= 0)
  281.     return_error(e_undefined);
  282.     if (!r_has_type(pcstr, t_string)) {
  283.     /*
  284.      * The ADOBEPS4 Windows driver replaces the .notdef entry of
  285.      * otherwise normal Type 1 fonts with the procedure
  286.      *    {pop 0 0 setcharwidth}
  287.      * To prevent this from making the font unembeddable in PDF files
  288.      * (with our present font-writing code), we recognize this as a
  289.      * special case and return a Type 1 CharString consisting of
  290.      *    0 0 hsbw endchar
  291.      * Note that we rely on garbage collection to free this string.
  292.      */
  293.     if (font->FontType == ft_encrypted &&
  294.         charstring_is_notdef_proc(pcstr)
  295.         )
  296.         return charstring_make_notdef(pstr, font);
  297.     else
  298.         return_error(e_typecheck);
  299.     }
  300.     pstr->data = pcstr->value.const_bytes;
  301.     pstr->size = r_size(pcstr);
  302.     return 0;
  303. }
  304. private bool
  305. charstring_is_notdef_proc(const ref *pcstr)
  306. {
  307.     if (r_is_array(pcstr) && r_size(pcstr) == 4) {
  308.     ref elts[4];
  309.     long i;
  310.  
  311.     for (i = 0; i < 4; ++i)
  312.         array_get(pcstr, i, &elts[i]);
  313.     if (r_has_type(&elts[0], t_name) &&
  314.         r_has_type(&elts[1], t_integer) && elts[1].value.intval == 0 &&
  315.         r_has_type(&elts[2], t_integer) && elts[2].value.intval == 0 &&
  316.         r_has_type(&elts[3], t_name)
  317.         ) {
  318.         ref nref;
  319.  
  320.         name_enter_string("pop", &nref);
  321.         if (name_eq(&elts[0], &nref)) {
  322.         name_enter_string("setcharwidth", &nref);
  323.         if (name_eq(&elts[3], &nref))
  324.             return true;
  325.         }
  326.     }
  327.     }
  328.     return false;
  329. }
  330. private int
  331. charstring_make_notdef(gs_const_string *pstr, const gs_font *font)
  332. {
  333.     const gs_font_type1 *const pfont = (const gs_font_type1 *)font;
  334.     static const byte char_data[4] = {
  335.     139,            /* 0 */
  336.     139,            /* 0 */
  337.     c1_hsbw,
  338.     cx_endchar
  339.     };
  340.     uint len = max(pfont->data.lenIV, 0) + sizeof(char_data);
  341.     byte *chars = gs_alloc_string(font->memory, len, "charstring_make_notdef");
  342.  
  343.     if (chars == 0)
  344.     return_error(e_VMerror);
  345.     pstr->data = chars;
  346.     pstr->size = len;
  347.     if (pfont->data.lenIV < 0)
  348.     memcpy(chars, char_data, sizeof(char_data));
  349.     else {
  350.     crypt_state state = crypt_charstring_seed;
  351.  
  352.     memcpy(chars + pfont->data.lenIV, char_data, sizeof(char_data));
  353.     gs_type1_encrypt(chars, chars, len, &state);
  354.     }
  355.     return 0;
  356. }
  357.  
  358. /*
  359.  * Enumerate the next glyph from a directory.  This is essentially a
  360.  * wrapper around dict_first/dict_next to implement the enumerate_glyph
  361.  * font procedure.
  362.  */
  363. int
  364. zchar_enumerate_glyph(const ref *prdict, int *pindex, gs_glyph *pglyph)
  365. {
  366.     int index = *pindex - 1;
  367.     ref elt[2];
  368.  
  369.     if (index < 0)
  370.     index = dict_first(prdict);
  371. next:
  372.     index = dict_next(prdict, index, elt);
  373.     *pindex = index + 1;
  374.     if (index >= 0) {
  375.     switch (r_type(elt)) {
  376.         case t_integer:
  377.         *pglyph = gs_min_cid_glyph + elt[0].value.intval;
  378.         break;
  379.         case t_name:
  380.         *pglyph = name_index(elt);
  381.         break;
  382.         default:        /* can't handle it */
  383.         goto next;
  384.     }
  385.     }
  386.     return 0;
  387. }
  388.